package com.yuexun.beilunpatient.ui.test.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.test.adapter.TestAdapter;
import com.yuexun.beilunpatient.ui.test.bean.AssayResp;
import com.yuexun.beilunpatient.ui.test.bean.JtRmyyAssayResult;
import com.yuexun.beilunpatient.ui.test.model.impl.TestModel;
import com.yuexun.beilunpatient.ui.test.presenter.impl.TestPresenter;
import com.yuexun.beilunpatient.ui.test.ui.view.ITestView;
import com.yuexun.beilunpatient.widget.EmptyLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.FooterLoadingLayout;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshBase;
import com.yuexun.beilunpatient.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/9.
 */
public class Act_Test_ScanList extends BaseKJActivity implements ITestView {
    @Bind(R.id.list)
    PullToRefreshList pullList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    private TestPresenter presenter;
    ListView listView;
    TestAdapter adapter;
    private int nowpage = 1;
    private int pagesize = 20;
    ArrayList<AssayResp> assayResps = new ArrayList<>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_test);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new TestPresenter(this, new TestModel());

        initList();
        ApiGetData();
    }

    /**
     * 初始化列表
     */
    private void initList() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                nowpage = 1;
                ApiGetData();
            }
        });

        listView = pullList.getRefreshView();
        listView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        pullList.setPullRefreshEnabled(true);
        pullList.setPullLoadEnabled(true);
        ((FooterLoadingLayout) pullList.getFooterLoadingLayout()).setNoMoreDataText("无更多数据");

        adapter = new TestAdapter(listView, assayResps, R.layout.item_testlist);
        listView.setAdapter(adapter);

        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = 1;
                ApiGetData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                nowpage = nowpage + 1;
                ApiGetData();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AssayResp item = (AssayResp) adapterView.getItemAtPosition(i);
                if (item.getBarcode() != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("barcode", item.getBarcode() + "");
                    bundle.putString("report_no", item.getReport_no());
                    Intent intent = new Intent(aty, Act_Test_ScanDetail.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    ViewInject.toast("未出报告单!");
                }
            }
        });
    }

    /**
     * 请请求数据
     */
    private void ApiGetData() {
        if (SystemTool.checkNet(aty)) {
            Map<String, String> params = new HashMap<>();
            params.put("cardNo", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.ID_CARD));
            params.put("patientName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_NAME));
            params.put("pageIndex", nowpage + "");
            params.put("pageSize", pagesize + "");
            presenter.inquireBlrmyyAssayPageList(params);
        } else {
            ViewInject.toast("网络异常，请检查网络是否连接!");
        }
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showTestScanList(BaseEntity<JtRmyyAssayResult> bean) {
        if (pullList.isPullRefreshEnabled()) {
            pullList.onPullDownRefreshComplete();
        }
        if (bean.getStatus().equals("200")) {
            if (bean.getDatas().size() == 0) {
                pullList.setHasMoreData(false);
            } else {
                if (pullList.isPullLoadEnabled()) {
                    pullList.onPullUpRefreshComplete();
                }
            }

            if (nowpage == 1) {
                assayResps.clear();
                assayResps = bean.getDatas().get(0).getAssayResps();
            } else {
                assayResps.addAll(bean.getDatas().get(0).getAssayResps());
            }
            adapter.refresh(assayResps);
        }else {
            ViewInject.toast(bean.getMessage());
        }

        if (assayResps.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
